package com.phiture.erp.basic.core.service.impl;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.phiture.erp.basic.common.utils.BasicCodeGenerator;
import com.phiture.erp.basic.core.pojo.dto.ErpCustomerPageReqDTO;
import com.phiture.erp.basic.core.pojo.dto.ErpCustomerSaveReqDTO;
import com.phiture.erp.basic.core.service.ErpCustomerService;
import com.phiture.erp.basic.dal.entity.ErpCustomerDO;
import com.phiture.erp.basic.dal.mapper.ErpCustomerMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static com.phiture.erp.common.constants.ErrorCodeConstants.CUSTOMER_NOT_ENABLE;
import static com.phiture.erp.common.constants.ErrorCodeConstants.CUSTOMER_NOT_EXISTS;

/**
 * @author YH
 * @date 2025/3/28
 */

@Service
@RequiredArgsConstructor
public class ErpCustomerServiceImpl implements ErpCustomerService {
    private final ErpCustomerMapper customerMapper;

    @Override
    public Long createCustomer(ErpCustomerSaveReqDTO createReqDTO) {
        ErpCustomerDO customer = BeanUtils.toBean(createReqDTO, ErpCustomerDO.class);
        customer.setCustomerCode(BasicCodeGenerator.generateCustomerCode());
        customerMapper.insert(customer);
        return customer.getId();
    }

    @Override
    public void updateCustomer(ErpCustomerSaveReqDTO updateReqDTO) {
        // 校验存在
        validateCustomerExists(updateReqDTO.getId());
        // 更新
        ErpCustomerDO updateObj = BeanUtils.toBean(updateReqDTO, ErpCustomerDO.class);
        customerMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomer(Long id) {
        // 校验存在
        validateCustomerExists(id);
        // 删除
        customerMapper.deleteById(id);
    }

    @Override
    public ErpCustomerDO getCustomer(Long id) {
        return customerMapper.selectById(id);
    }

    @Override
    public ErpCustomerDO validateCustomer(Long id) {
        ErpCustomerDO customer = customerMapper.selectById(id);
        if (customer == null) {
            throw exception(CUSTOMER_NOT_EXISTS);
        }
        if (CommonStatusEnum.isDisable(customer.getStatus())) {
            throw exception(CUSTOMER_NOT_ENABLE, customer.getName());
        }
        return customer;
    }

    @Override
    public List<ErpCustomerDO> getCustomerList(Collection<Long> ids) {
        return customerMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ErpCustomerDO> getCustomerPage(ErpCustomerPageReqDTO pageReqDTO) {
        return customerMapper.selectPage(pageReqDTO, new LambdaQueryWrapperX<ErpCustomerDO>()
                .likeIfPresent(ErpCustomerDO::getName, pageReqDTO.getName())
                .eqIfPresent(ErpCustomerDO::getMobile, pageReqDTO.getMobile())
                .eqIfPresent(ErpCustomerDO::getCustomerCode, pageReqDTO.getCustomerCode())
                .eqIfPresent(ErpCustomerDO::getTelephone, pageReqDTO.getTelephone())
                .orderByDesc(ErpCustomerDO::getId));
    }

    @Override
    public List<ErpCustomerDO> getCustomerListByStatus(Integer status) {
        return customerMapper.selectList(ErpCustomerDO::getStatus, status);
    }

    @Override
    public Map<Long, String> getCustomerMap() {
        List<ErpCustomerDO> list = customerMapper.selectList(
                Wrappers.lambdaQuery(ErpCustomerDO.class)
                        .eq(ErpCustomerDO::getStatus, CommonStatusEnum.ENABLE.getStatus())
                        .eq(ErpCustomerDO::getDeleted, Boolean.FALSE)
        );
        return convertMap(list, ErpCustomerDO::getId, ErpCustomerDO::getName);
    }

    private void validateCustomerExists(Long id) {
        if (customerMapper.selectById(id) == null) {
            throw exception(CUSTOMER_NOT_EXISTS);
        }
    }
}
